//
//  XMLatelyVisitorsView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/21.
//

#import "XMLatelyVisitorsView.h"

@interface XMLatelyVisitorsCell ()

@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation XMLatelyVisitorsCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.avatarImgView];
    [self addSubview:self.nameLabel];
}

- (void)setContentWithUserModel:(XMLatelyVisitorModel *)userModel {
//    _avatarImgView.image = [UIImage imageWithColor:kColorHEX(0x00ffff)];
//    _nameLabel.text = @"刘安迪";
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:userModel.visitorAvatar]
                      placeholderImage:ImageNamed(@"mine_avatar_icon")];
    _nameLabel.text = userModel.visitorNickname;
}

- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), (KScaleWidth(77.0) - KScaleWidth(46.0))/2, KScaleWidth(46.0), KScaleWidth(46.0))];
//        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
        [_avatarImgView setRadius:_avatarImgView.height/2 borderColor:kColorHEX(0xf0f0f0) borderWidth:1];
        _avatarImgView.image = ImageNamed(@"mine_avatar_icon");
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(75.0), 0, self.width - KScaleWidth(75.0) - KScaleWidth(100.0), KScaleWidth(77.0)) font:KBoldFontSize(15.0) textColor:kColorHEX(0x333333)];
    }
    return _nameLabel;
}

@end

@interface XMLatelyVisitorsView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;

@property (nonatomic, strong) NSMutableArray *userModelArr;

@end

@implementation XMLatelyVisitorsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"最近访客";
    
    self.backgroundColor = kColorHEX(0xF0F0F0);
    
    [self addSubview:self.tbl];
}

- (void)setContentWithUserModelArr:(NSArray *)userModelArr
                              page:(NSInteger)page {
    [_tbl.mj_header endRefreshing];
    if (page == 0) {
        [self.userModelArr removeAllObjects];
    }
    if (userModelArr.count >= 10) {
        [_tbl.mj_footer endRefreshing];
    }
    else {
        [_tbl.mj_footer endRefreshingWithNoMoreData];
    }
    [self.userModelArr addObjectsFromArray:userModelArr];
    [_tbl reloadData];
}

#pragma mark - Delegate
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return self.userModelArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return KScaleWidth(57.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(57.0))];
    headView.backgroundColor = kColorHEX(0xffffff);
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), 0, headView.width - KScaleWidth(16.0)*2, headView.height) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
    titleLabel.text = @"仅显示30天内容访客，访客记录仅你可见";
    [headView addSubview:titleLabel];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(77.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMLatelyVisitorsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMLatelyVisitorsCellName" forIndexPath:indexPath];
    [cell setContentWithUserModel:self.userModelArr[indexPath.row]];
//    static NSString *cellName = @"XMLatelyVisitorCellName";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName];
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    }
//    cell.imageView.image = ImageNamed(@"mine_avatar_icon");
//    cell.textLabel.text = @"刘安迪";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkUser)]) {
        [self.delegate checkUser];
    }
}

#pragma mark - Init
- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH)];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.backgroundColor = kColorHEX(0xF0F0F0);
        _tbl.separatorInset = UIEdgeInsetsMake(0, KScaleWidth(72.0), 0, 0);
        [_tbl registerClass:[XMLatelyVisitorsCell class] forCellReuseIdentifier:@"XMLatelyVisitorsCellName"];
        XMWeakSelf;
        _tbl.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshData)]) {
                [weakSelf.delegate refreshData];
            }
        }];
        
        _tbl.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(loadMore)]) {
                [weakSelf.delegate loadMore];
            }
        }];
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

- (NSMutableArray *)userModelArr {
    if (!_userModelArr) {
        _userModelArr = [NSMutableArray array];
    }
    return _userModelArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
